var baseUrl = $('#baseUrl').val();

var MetadataSet = {

	changeInput:function(){

	    var inputType = $("#inputType").val();
	    var defaultValueLabel = $("#defaultValueLabel").val();
	    var html = "";
	    $("#defaultValueDiv").html("");
	    $("#metadataOptionDiv").show();
            $("#metadataOptionBody").html("");
	    switch(inputType){
		    case '1':
		    case '5':
		        html += '<div class="form_line_div">';
		        html += '<div class="form_element_label" style="width:150px;">' + defaultValueLabel + '</div>';
		        html += '<div class="form_element">';
		        html += "<input type='text' name='defaultValue' />";
		        html += '</div>';
		        html += '</div>';
		        html += '<div class="clear"/>';
		        $("#defaultValueDiv").html(html);
		        $("#metadataOptionDiv").hide();
		        break;
		    case '2':
		        html += '<div class="form_line_div">';
		        html += '<div class="form_element_label" style="width:150px;">' + defaultValueLabel + '</div>';
		        html += '<div class="form_element">';
		        html += "<textarea type='text' name='defaultValue' rows=7 cols=35 ></textarea>";
		        html += '</div>';
		        html += '</div>';
		        html += '<div class="clear"/>';
		        $("#defaultValueDiv").html(html);
		        $("#metadataOptionDiv").hide();
		        break;
		    	
	    }
	},

	addOption : function() {
		var inputType = $("#inputType").val();
		var removeBtn = $("#removeBtn").val();
		var optionCount = $("#optionCount").val();
		var trHtml = "<tr id='itemRow" + optionCount + "'>";
		trHtml += "<td class='dbgrid_td'>";
		trHtml += "<input type='hidden' value='0' name='optionId[]' />";
		trHtml += "</td>";		
		trHtml += "<td class='dbgrid_td'>";
		if(inputType == '6'){
			var html = $('#color_select_element').html();
			trHtml += html;
		}else{
			trHtml += "<input type='text' name='optionValue[]' style='width:180px;' />";
		}
		trHtml += "</td>";
		trHtml += "<td class='dbgrid_td'>";
		trHtml += "<input type='text' name='optionTitle[]' style='width:180px;' />";
		trHtml += "</td>";
		trHtml += "<td class='dbgrid_td'>";
		trHtml += "<input type='text' name='optionOrder[]' style='width:40px;' />";
		trHtml += "</td>";
		trHtml += "<td class='dbgrid_td'>";
		trHtml += "<input type='button' value='" + removeBtn
				+ "' onclick='javascript:MetadataSet.removeOption(\"itemRow"
				+ optionCount + "\",\"0\");' />";
		trHtml += "</td>";
		trHtml += "</tr>";
		$("#metadataOptionBody").append(trHtml);
		optionCount = parseInt(optionCount)+1;
		$("#optionCount").val(optionCount);
	},

	removeOption : function(index,optionId) {
		if(optionId != 0){
			var deleteOptionId = $("#deleteOptionId").val();
			deleteOptionId += "," + optionId;
			$("#deleteOptionId").val(deleteOptionId);
		}
		$("#" + index).remove();
	},

	openSearchDialog : function(baseUrl) {
		var url = baseUrl + "/metadata/set/searchrelate";
		Common.showDialog( {
			url : url,
			height : 450,
			width : 930,
			dataDiv : "searchDialog"
		});
	},
	deleteMetadata : function(id) {
		$('#setmetadata').find("input[value='" + id + "']").parent().remove();
	},
	getReturn : function(data) {
		var html = '';
		$("#searchDialog")
				.find("input[name='dbgrid_item_ckb']")
				.each(
						function(i, obj) {

							if ($(this).attr('checked') == true) {
								var same = false;
								$('#setmetadata').find(
										"input[name='dbgrid_item_ckb']")
										.each(
												function(j, objSet) {
													if ($(objSet).val() == $(
															obj).val()) {
														same = true;
													}
												});
								if (!same) {
									alert($(this).val());
									html += '<tr onmouseout="javascript:DBGrid.changeRowStyle(this,0)" onmouseover="javascript:DBGrid.changeRowStyle(this,1)" style="background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">';
									html += '<input type="hidden" value="'
											+ $(this).val()
											+ '" name="metadata_id[]"/><td class="dbgrid_td_ckb"><input type="checkbox" name="dbgrid_item_ckb" value="'
											+ $(this).val() + '"/></td>';
									html += '<td class="dbgrid_td">' + $(this)
											.parent().next().text() + '</td>';
									html += '<td class="dbgrid_td">' + $(this)
											.parent().next().next().text() + '</td>';
									html += '<td class="dbgrid_td">' + $(this)
											.parent().next().next().next()
											.text() + '</td>';
									html += '<td class="dbgrid_td"><a href="javascript:MetadataSet.deleteMetadata(' + $(
											this).val() + ')">delete</a></td>';
									html += '</tr>';
								}
							}
						});
		$('tbody').append(html);
		Common.closeDialog("searchDialog");
	},
	initSetTree : function(treeId, xml, imagePath, checkStatus) {
		var tree = new dhtmlXTreeObject(treeId, "100%", "100%", 0);
		tree.setImagePath(imagePath);
		tree.enableCheckBoxes(checkStatus);
		tree.enableTreeImages(0);
		tree.enableThreeStateCheckboxes(0);
		tree.loadXMLString(xml);
		return tree;
	},

	clickTreeNode : function(eventId) {
		var idArray = eventId.split('_');
		var baseUrl = $('#baseUrl').val();
		var url = '';
		var data = '';
		switch (idArray[0].toLowerCase()) {
		case 'set':
			url = baseUrl + '/metadata/set/editset/';
			break;
		case 'group':
			url = baseUrl + '/metadata/set/editgroup/';
			break;
		case 'metadata':
			url = baseUrl + '/metadata/set/editmetadata/';
			break;

		}
		MetadataSet.enableButton(idArray[0].toLowerCase());
		data = 'id=' + idArray[1];
		$('#TreeNodeEventId').val(eventId);
		$.ajax( {
			type : "POST",
			url : url,
			data : data,
			dataType : "Html",
			success : function(responseText) {
				$("#metadata_tree_form_content").empty();
				$("#metadata_tree_form_content").html(responseText);
			}
		});
	},

	showAddForm : function(type) {
		var baseUrl = $('#baseUrl').val();
		var data = 'parentId=' + $('#TreeNodeEventId').val();
		switch (type) {
		case 'set':
			url = baseUrl + '/metadata/set/addset/';
			break;
		case 'group':
			url = baseUrl + '/metadata/set/addgroup/';
			break;
		case 'metadata':
			url = baseUrl + '/metadata/set/addmetadata/';
			break;
		}
		$.ajax( {
			type : "POST",
			url : url,
			data : data,
			dataType : "Html",
			success : function(responseText) {
				$("#metadata_tree_form_content").empty();
				$("#metadata_tree_form_content").html(responseText);
			}
		});
	},
	deleteElement : function(type) {
		var baseUrl = $('#baseUrl').val();
		var idArray = $('#TreeNodeEventId').val().split('_');
		var alertTitle = $('#alertTitle').val();
		var alertMessage = $('#alertDelMsg').val();
		switch (type) {
		case 'set':
			url = baseUrl + '/metadata/set/deleteset/id/' + idArray[1];
			break;
		case 'group':
			url = baseUrl + '/metadata/set/deletegroup/id/' + idArray[1];
			break;
		case 'metadata':
			url = baseUrl + '/metadata/set/deletemetadata/id/' + idArray[1];
			break;
		}
		$.ajax( {
			type : "POST",
			url : url,
			dataType : "Html",
			success : function(responseText) {

				$("#metadata_tree_form_content").empty();
				$("#metadata_tree_form_content").html(responseText);
				MetadataSet.resetTree();
				if (type == 'set') {
					jAlert(alertMessage, '' , function(){window.location = baseUrl + '/metadata/set';});
				}else{
					jAlert(alertMessage, '');
				}
				
			}
		});
	},
	submitAddForm : function(type) {
		var baseUrl = $('#baseUrl').val();
		var url = '';
		var data = '';
		var alertTitle = $('#alertTitle').val();
		var alertMessage = $('#alertInsertMsg').val();
		data = 'parentId=' + $('#TreeNodeEventId').val() + '&';
		switch (type) {
		case 'set':
			url = baseUrl + '/metadata/set/subaddset/';
			break;
		case 'group':
			url = baseUrl + '/metadata/set/subaddgroup/';
			break;
		case 'metadata':
			url = baseUrl + '/metadata/set/subaddmetadata/';
			break;
		}
		data = data + $('#addform').serialize();
		$.ajax( {
			type : "POST",
			url : url,
			data : data,
			dataType : "text",
			success : function(response) {
				$("#metadata_tree_form_content").empty();
				if(response>0){
					if (type == 'set') {
						window.location = window.location + '/id/'
								+ response;
					}
					jAlert(alertMessage, '');
				}else if(response=='ok'){
					jAlert(alertMessage, '');
				}else{
					$("#metadata_tree_form_content").html(response);
				}

				MetadataSet.resetTree();
			}
		});
	},
	submitEditForm : function(type) {
		var baseUrl = $('#baseUrl').val();
		var url = '';
		var data = '';
		var alertTitle = $('#alertTitle').val();
		var alertMessage = $('#alertUpdateMsg').val();
		switch (type) {
		case 'set':
			url = baseUrl + '/metadata/set/subeditset/';
			break;
		case 'group':
			url = baseUrl + '/metadata/set/subeditgroup/';
			break;
		case 'metadata':
			url = baseUrl + '/metadata/set/subeditmetadata/';
			data = 'inputType='+$('#inputType').val()+'&';
			break;
		}
		data = data + $('#addform').serialize();
		
		$.ajax( {
			type : "POST",
			url : url,
			data : data,
			dataType : "text",
			success : function(response) {
				$("#metadata_tree_form_content").empty();
				if (response=='ok') {
					jAlert(alertMessage, '');
				}else{
					$("#metadata_tree_form_content").html(response);
				}
				MetadataSet.resetTree();
			}
		});
	},

	resetTree : function() {
		var baseUrl = $('#baseUrl').val();
		var url = baseUrl + '/metadata/set/gettreexml/id/' + $('#setId').val();

		data = $('#addform').serialize();
		$.ajax( {
			type : "POST",
			url : url,
			success : function(xml) {
				var imagePath = baseUrl + "/images/dhtmltreeimgs/";
				$('#treeboxbox_tree').empty();
				tree = MetadataSet.initSetTree("treeboxbox_tree", xml,
						imagePath, 0);
				tree.setOnClickHandler(MetadataSet.clickTreeNode);
				tree.openAllItems("set_" + $('#setId').val());
			}
		});
	},

	enableButton : function(type) {
		var url = $('#baseUrl').val();
		$('#add_set_img').attr('src', url + '/images/add_disable.png');
		$('#add_group_img').attr('src', url + '/images/add_disable.png');
		$('#add_metadata_img').attr('src', url + '/images/add_disable.png');
		$('.metadata_tree_form_tree > .center_form_btn_div').unbind('click');
		$('.metadata_tree_form_tree > .center_form_btn_div').css('cursor','default');
		switch (type) {
		case 'set':

			$('#add_group_btn_div').click(function() {
				MetadataSet.showAddForm('group');
			});
			$('#add_group_btn_div').css('cursor','pointer');
			$('#add_group_img').attr('src', url + '/images/add.gif');
			break;
		case 'group':

			$('#add_metadata_btn_div').click(function() {
				MetadataSet.showAddForm('metadata');
			});
			$('#add_metadata_btn_div').css('cursor','pointer');
			$('#add_metadata_img').attr('src', url + '/images/add.gif');
			break;
		case 'metadata':
			break;
		}

	}
}